<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>promise请求数据</h1>
  <button id="btn">点击请求数据</button>
  <script>
    // 获取元素
    const obtn = document.getElementById('btn')

    // 绑定点击事件
    obtn.onclick = function () {
      new Promise((resolve, reject) => {
          try {
            console.log('开始获取数据A')
            // throw new Error('数据A获取失败')
            setTimeout(() => {
              console.log('数据A获取成功')
              const name = {
                name: '程龙'
              }
              resolve(name)
            }, 2000)
          } catch (e) {
            reject(e.message)
          }
        })

        .then((value) => {
          return new Promise((resolve, reject) => {
            try {
              console.log('开始获取数据B')
              // throw new Error('数据B获取失败')
              setTimeout(() => {
                console.log('数据B获取成功')
                const age = {
                  age: 20
                }
                resolve({
                  ...value,
                  ...age
                })
              }, 2000)
            } catch (e) {
              reject(e.message)
            }
          })
        })

        .then((value) => {
          return new Promise((resolve, reject) => {
            try {
              console.log('开始获取数据C')
              // throw new Error('数据C获取失败')
              setTimeout(() => {
                console.log('数据C获取成功')
                const sex = {
                  sex: '男'
                }
                resolve({
                  ...value,
                  ...sex
                })
              }, 2000)
            } catch (e) {
              reject(e.message)
            }
          })
        })

        .then((value) => {
          console.log(value);
        })

        .catch((re) => {
          console.log('获取数据失败');
        })
    }
  </script>
</body>

</html>